﻿/*
ParallelPlatform Project - A web platform to distribute and share knowledge
Online Editor - An online tool to create and play animations
Copyright (C) 2011 Daniel Guerrero Martínez & Sergio Rodríguez Lumley

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package Classes{

	import flash.events.MouseEvent;
	
	public class CanvasRemoverHandler extends CanvasDefaultHandler {
		
		/**
		 Constructor
		*/
		function CanvasRemoverHandler(c:CanvasCore){
			super(c);
		}
		
		override public function activate(param=null):void{
			if(! activated){
				activated = true;
				core.showBlockPanel(false); // Do not show block panel
			}
		}
		
		override public function clickShape(evt:MouseEvent, obj:ShapeObject, type:int):void{
			if(type == ShapeObject.SINGLECLICK){
				
				// if the object was not selected before
				if(core.getSelectedShapes().indexOf(obj) < 0){
					removeShape(obj);
					core.removeSelections();
				}else{
					var first:Boolean = true;
					for each (var shap:ShapeObject in core.getSelectedShapes()){
						removeShape(shap, first);
						if(first) first = false;
					}
					core.removeSelections();
				}
				
			}else if(type == ShapeObject.DOUBLECLICK){
				if(obj.isRemoved()){
					obj.unremove();
					core.addAction(
						new Action(Action.INVALIDATE_REMOVE, obj.getFullID())
						);
				}
			}
		}
		
		override public function clickBack(evt:MouseEvent):void{
			core.removeSelections();
		}
		
		// --------------- PRIVATE FUNCTIONS ---------------//
		
		/**
		  Removes the specified shape from stage, if it was created in this
		  same stage, it will be completely removed, otherwise it will only
		  be marked as deleted
		  @param obj ShapeObject to delete
		  @param firstClusterAction Boolean to indicate if this action is the
		  	first of a series of chained actions, or if it is only this one
			linked
		*/
		private function removeShape(obj:ShapeObject, firstClusterAction:Boolean = true):void{
			// If the object was created on the same scene
			// we remove it completely
			if(obj.getStageID() == core.getCurrent()){
				core.getContainer(core.getCurrent()).removeShape(obj);
			}else{ // if not, we just mark it as deleted
				obj.remove();
			}
			
			//An object has been removed from scene
			//REMOVE, "fullID"
			core.addAction(new Action(
				Action.REMOVE,
				obj.getFullID() ), firstClusterAction);
		}

	}
}